package com.xz.http.instance;

import com.xz.bean.LinkReg;
import com.xz.queue.Consumer;
import com.xz.queue.Producer;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by YLZX-Z0107 on 2014/7/15.
 */
public class Client {
    static Set<String> detailSet = new HashSet<String>(200);
    static CloseableHttpClient client = ClientSingleton.getInstance().getClient();

    static HttpGet get = HttpFactory.getMethod();
    public static void main(String[] args) {
        BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);
        Producer producer1 = new Producer(queue);
        Producer producer2 = new Producer(queue);
        Producer producer3 = new Producer(queue);
        Consumer consumer = new Consumer(queue);
        // 借助Executors
        ExecutorService service = Executors.newCachedThreadPool();
        // 启动线程
        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        service.execute(consumer);
    }

    public static void searchStart(String initUrl){
        setUri(get, initUrl);
        get.addHeader("Cookie","PHPSESSID=go95798enia09k0vcc6k9ao3n0");
        String content = getResponseContent(client, get);
        Document document = Jsoup.parse(content);
        Elements elements = document.select("a[href]");
        LinkReg reg = new LinkReg();
        reg.setLinkCon("item_");
        for (Element element : elements) {
            if (isCorrect(element, reg)) {
                String detail = element.attr("href");
                detailSet.add(getAbsUrl(detail));
//                System.out.println(element.tagName() + "---" + getAbsUrl(detail) + "---" + element.text());
            }
        }
        System.out.println(detailSet.size());
    }

    public static void setUri(HttpGet get, String uri) {
        try {
            get.setURI(new URI(uri));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public static String getResponseContent(CloseableHttpClient client, HttpRequestBase method) {
        String result = null;
        try {
            CloseableHttpResponse response = client.execute(method);
            result = EntityUtils.toString(response.getEntity(),"utf-8");
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                System.out.println("get success!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void printLink(String content) {
        printLink(content, null);
    }

    public static void printLink(String content, LinkReg reg) {
        Document document = Jsoup.parse(content);
        Elements elements = document.select("a[href]");
        for (Element element : elements) {
            if (isCorrect(element, reg))
                System.out.println(element.tagName() + "---" + getAbsUrl(element.attr("href")) + "---" + element.text());
        }
    }

    private static boolean isCorrect(Element element, LinkReg reg) {
        boolean cur = true;
        if (reg != null && reg.getLinkUnCon() != null) {
            String[] linkUnCon = reg.getLinkUnCon().split(",");
            for (String link : linkUnCon) {
                if (element.attr("href").contains(link)) {
                    return false;
                }
            }
        }
        if (reg != null && reg.getLinkCon() != null) {
            String[] linkCon = reg.getLinkCon().split(",");
            for (String link : linkCon) {
                if (!element.attr("href").contains(link)) {
                    return false;
                }
            }
        }
        if(StringUtils.isEmpty(element.text())){
            return false;
        }
        return cur;
    }

    public static String getAbsUrl(String url){
        if(url.indexOf("/")==0){
            return "http://www.arzon.jp"+url;
        }
        return url;
    }
}
